package com.team2.hospital.utils;

import com.team2.hospital.entity.Orders;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class PdfUtil {

    /**
     * 导出医院病情报告单为PDF文件
     *
     * @param request  HTTP请求对象
     * @param response HTTP响应对象
     * @param order    订单对象，包含病人的相关信息和病情记录
     * @throws Exception 如果生成PDF过程中发生异常
     */
    public static void ExportPdf(HttpServletRequest request, HttpServletResponse response, Orders order) throws Exception {
        // 设置响应头，告诉浏览器生成PDF文件
        response.setHeader("content-Type", "application/pdf");

        // 默认文件名（可自定义）
        // response.setHeader("Content-Disposition", "attachment;filename=XXX.pdf");

        // 设置中文字体支持
        BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);

        // 创建文档对象，设置页面大小为A4
        Document document = new Document(PageSize.A4);

        // 创建标题段落，并设置样式
        Paragraph titleParagraph = new Paragraph();
        titleParagraph.setFont(new Font(bfChinese, 20, Font.NORMAL)); // 设置标题字体
        titleParagraph.setAlignment(Element.ALIGN_CENTER); // 设置居中对齐
        titleParagraph.add("医院病情报告单"); // 标题内容

        // 创建打印时间段落
        Paragraph tipsParagraph = new Paragraph();
        tipsParagraph.setFont(new Font(bfChinese, 10, Font.NORMAL)); // 设置字体
        tipsParagraph.setAlignment(Element.ALIGN_CENTER); // 设置居中对齐
        tipsParagraph.setLeading(tipsParagraph.getTotalLeading() + 10); // 设置行距
        tipsParagraph.add("打印时间：" + TodayUtil.getTodayYmd()); // 添加打印日期

        // 创建PDF写入器
        PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());

        // 打开文档进行内容添加
        document.open();

        // 设置PDF文档元数据
        document.addTitle("医院");
        document.addAuthor("医院");
        document.addCreationDate();
        document.addKeywords("iText");
        document.addLanguage("中文");

        // 向文档中添加标题和打印时间段落
        document.add(titleParagraph);
        document.add(tipsParagraph);

        // 创建病人基本信息表格
        PdfPTable tableMessage = new PdfPTable(4);
        tableMessage.setSpacingBefore(8f); // 设置表格上方间距
        tableMessage.setSpacingAfter(8f); // 设置表格下方间距

        // 设置表格单元格无边框，并设置表格宽度比例
        tableMessage.getDefaultCell().setBorder(0);
        tableMessage.setTotalWidth(new float[]{30, 120, 30, 120});

        // 填充病人信息表格
        tableMessage.addCell(new Paragraph("姓名", FontChinese));
        tableMessage.addCell(new Paragraph(order.getPatient().getPName(), FontChinese));
        tableMessage.addCell(new Paragraph("性别", FontChinese));
        tableMessage.addCell(new Paragraph(order.getPatient().getPGender(), FontChinese));
        tableMessage.addCell(new Paragraph("年龄", FontChinese));
        tableMessage.addCell(new Paragraph(order.getPatient().getPAge() + " 岁", FontChinese));
        tableMessage.addCell(new Paragraph("单号", FontChinese));
        tableMessage.addCell(String.valueOf(order.getOId()));
        tableMessage.addCell(new Paragraph("日期", FontChinese));
        tableMessage.addCell(order.getOEnd());
        tableMessage.addCell(new Paragraph("电话", FontChinese));
        tableMessage.addCell(order.getPatient().getPPhone());

        // 将病人信息表格添加到文档中
        document.add(tableMessage);

        // 创建病情详细信息表格
        PdfPTable tableOrder = new PdfPTable(1);
        tableOrder.getDefaultCell().setBorder(0); // 设置无边框
        tableOrder.setSpacingBefore(30f); // 设置表格上方间距
        tableOrder.setSpacingAfter(10f); // 设置表格下方间距

        // 症状信息
        PdfPCell cell1 = new PdfPCell(new Paragraph("症状", new Font(bfChinese, 14, Font.NORMAL)));
        cell1.setFixedHeight(25);
        cell1.setBorder(0);

        PdfPCell cell2 = new PdfPCell(new Paragraph(order.getORecord(), new Font(bfChinese, 10, Font.NORMAL)));
        cell2.setFixedHeight(30);
        cell2.setBorder(0);
        cell2.setPaddingLeft(10); // 设置左边距

        // 检查项目及价钱
        PdfPCell cell3 = new PdfPCell(new Paragraph("检查项目及价钱", new Font(bfChinese, 14, Font.NORMAL)));
        cell3.setFixedHeight(25);
        cell3.setBorder(0);

        PdfPCell cell4 = new PdfPCell(new Paragraph(order.getOCheck(), new Font(bfChinese, 10, Font.NORMAL)));
        cell4.setFixedHeight(30);
        cell4.setBorder(0);
        cell4.setPaddingLeft(10);

        // 药物及价钱
        PdfPCell cell5 = new PdfPCell(new Paragraph("药物及价钱", new Font(bfChinese, 14, Font.NORMAL)));
        cell5.setFixedHeight(25);
        cell5.setBorder(0);

        PdfPCell cell6 = new PdfPCell(new Paragraph(order.getODrug(), new Font(bfChinese, 10, Font.NORMAL)));
        cell6.setFixedHeight(30);
        cell6.setBorder(0);
        cell6.setPaddingLeft(10);

        // 诊断/医生意见
        PdfPCell cell7 = new PdfPCell(new Paragraph("诊断/医生意见", new Font(bfChinese, 14, Font.NORMAL)));
        cell7.setFixedHeight(25);
        cell7.setBorder(0);

        PdfPCell cell8 = new PdfPCell(new Paragraph(order.getOAdvice(), new Font(bfChinese, 10, Font.NORMAL)));
        cell8.setFixedHeight(100);
        cell8.setBorder(0);
        cell8.setPaddingLeft(10);

        // 将所有单元格添加到表格
        tableOrder.addCell(cell1);
        tableOrder.addCell(cell2);
        tableOrder.addCell(cell3);
        tableOrder.addCell(cell4);
        tableOrder.addCell(cell5);
        tableOrder.addCell(cell6);
        tableOrder.addCell(cell7);
        tableOrder.addCell(cell8);

        // 将病情信息表格添加到文档中
        document.add(tableOrder);

        // 设置PDF文档的底部版权说明
        PdfContentByte cb = writer.getDirectContent();
        BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
        cb.beginText();
        cb.setFontAndSize(bf, 11);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "该报告单仅供参考", 300, 40, 0);
        cb.setFontAndSize(bf, 13);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "版权医院所有", 300, 20, 0);
        cb.endText();

        // 关闭文档
        document.close();
    }
}
